package cn.edu.sjtu.se.dslab.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.component.design.Button;
import cn.edu.sjtu.se.dslab.dao.IButtonDao;
import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.project.BusZone;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IButtonService;

@Service("buttonService")
@Transactional
public class ButtonServiceImpl implements IButtonService {

	@Resource(name = "buttonDao")
	private IButtonDao buttonDao;
	@Resource(name="projectDao")
	private IProjectDao projectDao;
	
	
	@Override
	public boolean addButton(Button button, long projectId) {
		Project project = projectDao.queryById(projectId);
		if (project == null) throw new NullPointerException("Project id not found " + projectId);
		Button existButton = buttonDao.queryByCode(button.getCode(), projectId);
		if (null != existButton){
			return false;
		}
		button.setProject(project);
		buttonDao.save(button);
		return true;
	}

	@Override
	public Button updateButton(Button button) {
		//Button existButton = buttonDao.queryByCode(button.getCode());
		//there exist the same button code
		//if(button.getId() != existButton.getId()){
		//	return null;
		//}
		return buttonDao.update(button);
	}
	


	
	@Override
	public Button getButtonById(long id) {
		return buttonDao.queryById(id);
	}

	@Override
	public Button getButtonByName(String name) {
		return buttonDao.queryByName(name);
	}

	@Override
	public Button getButtonByCode(String code, long projectId) {
		return buttonDao.queryByCode(code, projectId);
	}

	@Override
	public List<Button> getButtonList() {
		return buttonDao.queryByList();
	}

	@Override
	public boolean deleteButtonById(long buttonId) {
		Button button = buttonDao.queryById(buttonId);
		if (null != button){
			buttonDao.deleteButton(button);
			return true;
		}
		return false;
	}

	@Override
	public List<Button> getButtonByProject(long projectId) {
		return buttonDao.queryByProject(projectId);
	}
	
	@Override
	public Button getButtonByProjectAndName(long projectId, String name){
		return buttonDao.queryByProjectAndName(projectId, name);
	}




}
